package ext.tianma.doc.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ext.tianma.util.PropertiesUtil;
import wt.util.WTProperties;

public class DCCWriteExcel {
	
	public static String sheetName = "DCC报表查询";
	public static String[] title = {"序号","料号","文档名称","型号","版号","存储位置","图纸类型","生效日期","拟制人","研发地","生产地","临时图纸","状态"};
	public static String configFilePath = "codebase" + File.separator + "ext" + File.separator + "tianma"
			+ File.separator + "cfg" + File.separator + "TianmaPro.properties";
	public static String ecportExcel(JSONArray array) throws IOException, JSONException {
		
		PropertiesUtil ut = new PropertiesUtil(configFilePath);
		String domain = ut.getValueByKey("system.domain");
		String folder = "TempExcel";
		WTProperties props = WTProperties.getLocalProperties();
		String filePath = props.getProperty("wt.codebase.location") + File.separator + "temp" + File.separator + folder + File.separator;
		File targerFile = new File(filePath);
		if(!targerFile.exists()) {
			targerFile.mkdir();
		}
		String pathAndName = filePath + "DCCStatementInquire.xls";
		String realPath = domain + File.separator + "temp" + File.separator + folder + File.separator + "DCCStatementInquire.xls";
		
		//1.创建HSSFWorkbook,对应一个excel
		
		HSSFWorkbook wb = new HSSFWorkbook();
		
		
		//2.在workbook中创建一个sheet，对应excel中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);
		
		//3.在sheet中添加表头第0行
		HSSFRow row = sheet.createRow(0);
		
		//4.创建单元格，并设置值表头 设置表头居中
	    HSSFCellStyle cellStyle = wb.createCellStyle();
	    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置居中
	    
	    // 声明列对象
	 	HSSFCell cell = null;
	 	
 		// 创建标题
	 	
 		for (int i = 0; i < title.length; i++) {
 			cell = row.createCell(i);
 			cell.setCellValue(title[i]);
 			cell.setCellStyle(cellStyle);
 		}
 		
 		//创建内容
 		
 		for(int i =0; i<array.length(); i++) {
 			HSSFRow tempRow = sheet.createRow(i+1);
 			JSONObject jsonObject = array.getJSONObject(i);
 			
 			for(int j =0;j<jsonObject.length()-1; j++) {
 				String tempCellName = row.getCell(j).getStringCellValue();
 				String tempValue = "";
 				if("序号".equals(tempCellName)) {
 					tempValue = String.valueOf(i+1);
 				}else if("料号".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("Item1");
 				}else if("文档名称".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("name");
 				}else if("型号".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("productType1");
 				}else if("版号".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("version");
 				}else if("存储位置".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("folder");
 				}else if("图纸类型".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("drawingType");
 				}else if("生效日期".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("releaseTime");
 				}else if("拟制人".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("updateBy");
 				}else if("研发地".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("modulesDevelopedLand1");
 				}else if("生产地".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("moduleOrigin1");
 				}else if("临时图纸".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("type");
 				}else if("状态".equals(tempCellName)) {
 					tempValue = (String) jsonObject.get("productState");
 				}
 				tempRow.createCell(j).setCellValue(tempValue);
 			}
 		}
 		//调整每一列宽度自适应
 		sheet.autoSizeColumn((short)0); 
        sheet.autoSizeColumn((short)1); 
        sheet.autoSizeColumn((short)2); 
        sheet.autoSizeColumn((short)3);
        sheet.autoSizeColumn((short)4);
        sheet.autoSizeColumn((short)5);
        sheet.autoSizeColumn((short)6);
        sheet.autoSizeColumn((short)7);
        sheet.autoSizeColumn((short)8);
        sheet.autoSizeColumn((short)9);
        sheet.autoSizeColumn((short)10);
        sheet.autoSizeColumn((short)11);
        sheet.autoSizeColumn((short)12);
	    
        FileOutputStream os = new FileOutputStream(pathAndName);
        wb.write(os);
		os.flush();
		os.close();
		return realPath;
	}
}
